package com.suntop.carousel.qlmediaplay.adapter;

import android.util.Log;

import com.suntop.carousel.qlmediaplay.field.Field_Banner;
import com.suntop.carousel.qlmediaplay.media.IMediaPlayEventListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

/**
 * 基础的片段适配器
 */
public abstract class BasicFragmentBannerAdapter<T> extends FragmentStateAdapter {
    private int needPage = Field_Banner.NORMAL_COUNT;//一页多屏个数
    private int sidePage = needPage / Field_Banner.NORMAL_COUNT;//边缘页面个数
    private ArrayList<T> dates=new ArrayList<>();
//    private HashMap<Integer,Fragment> hashMap=new HashMap<>();
    private IMediaPlayEventListener iMediaPlayEventListener;

    public BasicFragmentBannerAdapter<T> setNeedPage(int needPage) {
        this.needPage = needPage;
        return this;
    }

    public  BasicFragmentBannerAdapter<T> setSidePage(int sidePage) {
        this.sidePage = sidePage;
        return this;
    }

    public BasicFragmentBannerAdapter<T> setIMediaPlayEventListener(IMediaPlayEventListener iMediaPlayEventListener) {
        this.iMediaPlayEventListener = iMediaPlayEventListener;
        return this;
    }

    public BasicFragmentBannerAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    public BasicFragmentBannerAdapter(@NonNull Fragment fragment) {
        super(fragment);
    }

    public BasicFragmentBannerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }



    /**
     * 设置资源
     * @param newData
     */
    public void setSources(Collection<T> newData) {
        Log.e("设置资源",newData.size()+"");
        if (dates.size() > 0) {
            dates.clear();
//            hashMap.clear();
        }
        dates.addAll(newData);
        notifyItemRangeInserted(dates.size() - newData.size(), newData.size());

    }


    @Override
    public long getItemId(int position) {
        return super.getItemId(toRealPosition(position));
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(toRealPosition(position));
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        T t=dates.get(toRealPosition(position));
        Fragment createfragment = getCreatefragment(toRealPosition(position), t, iMediaPlayEventListener);
//        hashMap.put(toRealPosition(position),createfragment);
        return createfragment;
    }

    protected abstract Fragment getCreatefragment(int toRealPosition,T t,IMediaPlayEventListener listener);


    @Override
    public int getItemCount() {
        return dates.size()<=1?dates.size():(dates.size()+needPage);
    }

    public int realCount(){
        return dates.size();
    }

//    public Fragment  takeFragment(int position){
//      return   hashMap.get(position);
//    }


    public int toRealPosition(int position) {
        int realPosition = 0;
        if (realCount() > 1) {
            realPosition = (position - sidePage) % realCount();
        }
        if (realPosition < 0) {
            realPosition += realCount();
        }
        return realPosition;
    }
}
